/**
 * 
 */
package edu.rice.test.onedimension;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * @author frogy
 * 
 */
public class Sim {
	public Sim(double X, double T, int N) {
		Agent[] list = new Agent[N];
		for (int i = 0; i < N; i++) {
			list[i] = new Agent(X, list);
		}
		File dir = new File("Oned");
		dir.mkdir();
		for (int t = 0; t < T; t += 5) {
			System.out.println("Current time is " + t + "s");
			double[] den = new double[(int) X];
			double[] denl = new double[(int) X];
			double[] denr = new double[(int) X];
			for (int i = 0; i < N; i++) {
				den[list[i].getGridX()] += 1;
				if(Math.cos(list[i].getDir()) > 0){
					denr[list[i].getGridX()] += 1;
				} else {
					denl[list[i].getGridX()] += 1;
				}
				list[i].step(X);
			}
			for (int i = 0; i < N; i++) {
				list[i].updateOldValue();
			}
			if (t % 20 == 0) {
				File file = new File("Oned//den" + t);
				try {
					FileWriter fw = new FileWriter(file);
					PrintWriter pw = new PrintWriter(fw);
					for (int i = 0; i < (int) X; i++) {
						pw.println(den[i]);
					}
					pw.close();
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				File filel = new File("Oned//denl" + t);
				try {
					FileWriter fwl = new FileWriter(filel);
					PrintWriter pwl = new PrintWriter(fwl);
					for (int i = 0; i < (int) X; i++) {
						pwl.println(den[i]);
					}
					pwl.close();
					fwl.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				File filer = new File("Oned//denr" + t);
				try {
					FileWriter fwr = new FileWriter(filer);
					PrintWriter pwr = new PrintWriter(fwr);
					for (int i = 0; i < (int) X; i++) {
						pwr.println(den[i]);
					}
					pwr.close();
					fwr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		new Sim(500, 10000, 1000);
	}
}
